package com.controller;


import com.alibaba.fastjson.JSON;
import com.pojo.Room;
import com.pojo.User;
import com.service.RoomService;
import com.utils.PageSupport;
import com.vo.RoomVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@Controller
public class RoomController {

    @Autowired
    private RoomService roomService;

    @RequestMapping("/room/list.html")
    public String getroomList(RoomVO roomVO,Model model)
    {
        System.out.println(roomVO);
        int pageSize=5;
        int tempPageIndex=1;
        if (roomVO.getPageIndex()!=null){
            tempPageIndex=roomVO.getPageIndex();
        }

        if(roomVO.getPageIndex()==null||roomVO.getPageIndex()<1) {
            roomVO.setPageIndex(1);
        }

        // 符合条件的总数量
        int totalCount=roomService.getRoomCount(roomVO);
        System.out.println("totalCount="+totalCount);

        // 分页控制
        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(roomVO.getPageIndex());
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount=pages.getTotalPageCount();
        System.out.println(totalPageCount);
        if(roomVO.getPageIndex()>totalPageCount) {
            roomVO.setPageIndex(totalPageCount);
            tempPageIndex = totalPageCount;
        }

        // 获取用户列表
        List<Room> roomList=roomService.getRoomList(roomVO,pageSize);
        model.addAttribute("roomList",roomList);



        model.addAttribute("queryRoomName",roomVO.getQueryRoomName());
        model.addAttribute("queryRoomLocation",roomVO.getQueryRoomLocation());
        model.addAttribute("queryRoomMinNumber",roomVO.getQueryRoomMinNumber());
        model.addAttribute("queryRoomMaxNumber",roomVO.getQueryRoomMaxNumber());
        model.addAttribute("queryRoomStatus",roomVO.getQueryRoomStatus());
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        System.out.println(roomVO.getPageIndex());
        model.addAttribute("pageIndex",tempPageIndex);

        return "/room/list";
    }

    @RequestMapping(value="/room/add.html",method= RequestMethod.GET)
    public String toAddroom(Room room,Model model) {
        return "/room/add";
    }

    @RequestMapping(value="/room/add.html",method=RequestMethod.POST)
    public String doAddRoom(@Valid Room room, BindingResult bindingResult) {
        System.out.println("doAddRoom");
        if(bindingResult.hasErrors()) {
            System.out.println("666");
            return "/room/add";
        }

        boolean val=roomService.addRoom(room);
        if(val) {
            // 成功：转跳用户列表页面
            return "redirect:/room/list.html";
        }
        // 失败：返回添加用户页面
        return "redirect:/room/add.html";
    }

    @GetMapping(value="/room/view/{rid}")
    public String viewroom(@PathVariable Integer rid, Model model) {
        Room room=roomService.getRoomByRid(rid);
        model.addAttribute("room", room);
        return "/room/view";
    }

    @RequestMapping(value="/room/update.html/{rid}")
    public String updateRoom(@PathVariable Integer rid,
                             @ModelAttribute("room") Room room,
                             Model model) {
        System.out.println("updateRoom ID:"+rid);
        // 获取待修改用户信息
        room=roomService.getRoomByRid(rid);
        model.addAttribute("room", room);
        return "/room/modify";
    }

    @PostMapping(value="/room/update.html")
    public String doUpdateRoom(@Valid Room room,BindingResult bindingResult) {
        System.out.println("doUpdateRoom");

        if(bindingResult.hasErrors()) {
            System.out.println("666");
            return "/room/modify";
        }

        boolean val=roomService.updateRoomByRid(room);
        if(val) {
            System.out.println("success");
            // 成功：转跳用户列表页面
            return "redirect:/room/list.html";
        }
        System.out.println("failure");
        // 失败：返回添加用户页面
        return "redirect:/room/update.html?rid="+room.getId();
    }

    @RequestMapping("/room/delete/{rid}")
    @ResponseBody
    public String delete(@PathVariable Integer rid){

        HashMap<String,String> map = new HashMap<String, String>();

        Room room = roomService.getRoomByRid(rid);
        if(room!=null){
            try {
               roomService.deleteRoomByRid(rid);
                System.out.println("您成功删除了"+room);
                map.put("delResult","true");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("您没能删除"+room);
                map.put("delResult","false");
            }
        }else {
            System.out.println("该会议室不存在");
            map.put("deResulte","notexist");
        }
        return JSON.toJSONString(map);
    }
}
